package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class CourseServiceController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;

	private static Logger logger = LoggerFactory.getLogger(CourseServiceController.class);
	/*
	 * 预约记录接口 参数： 学员编号（stuId） 教练编号(teaId)
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/CourseAppointment", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String courseAppointment(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String stuId = request.getParameter("studentId");
			String teaId = request.getParameter("teacherId");
			String courseID = request.getParameter("courseID");
			String drivingType = request.getParameter("drivingType");
			String remark = request.getParameter("remark");
			if ((null != stuId && !stuId.isEmpty() && !stuId.equals("0"))
					&& (null != courseID && !courseID.isEmpty())
					&& (null != teaId && !teaId.isEmpty())) {
				paramsMap.put("stuId", stuId);
				paramsMap.put("teaId", teaId);
				paramsMap.put("Remark", remark);
				paramsMap.put("drivingType", drivingType);
				paramsMap.put("CourseID", courseID);
				response = bizService.courseAppoint(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空！");
			}
		} catch (Exception e) {
			logger.info("CourseAppointment" + e.toString());
			response.setErrorcode(1);
			response.setMessage("预约失败！");
		} finally {
			return response.toJSON();
		}
	}

	/*
	 * 取消预约记录接口 参数： 预约记录编号（Id）
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/deleteCourseAppointment", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String deleteCourseAppointment(
			HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String appointId = request.getParameter("appointId");
			String CourseID = request.getParameter("courseID");
			String remark = request.getParameter("remark");
			String stuId = request.getParameter("studentId");
			if ((appointId != null && !appointId.isEmpty())
					&& (CourseID != null && !CourseID.isEmpty())
					&& (stuId != null && !stuId.isEmpty())) {
				paramsMap.put("appointId", Integer.parseInt(appointId));
				paramsMap.put("remark", remark);
				paramsMap.put("stuId", stuId);
				paramsMap.put("CourseID", CourseID);
				response = bizService.deleteAppoint(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("缺少必填参数！");
			}
		} catch (Exception e) {
			logger.info("deleteCourseAppointment" + e.toString());
			response.setErrorcode(1);
			response.setMessage("取消预约失败！");
		} finally {
			return response.toJSON();
		}

	}
}
